home *** CD-ROM | disk | FTP | other *** search
/ Mastering Computers 3 / Mastering Computers Vol 3.iso / Win95 / Fun&Utils / STRTMENU.EXE / DELITEM.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1995-07-21  |  3.6 KB  |  132 lines

  1. /**************************************************************************
  2.  
  3.    THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF
  4.    ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO
  5.    THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
  6.    PARTICULAR PURPOSE.
  7.  
  8.    Copyright (C) 1993-1995  Microsoft Corporation.  All Rights Reserved.
  9.  
  10.    File:          DelItem.cpp
  11.    
  12.    Description:   Provides the functionality for deleting a shortcut 
  13.                   from the start menu.
  14.  
  15. **************************************************************************/
  16.  
  17. #define STRICT
  18.  
  19. /**************************************************************************
  20.    Include Files
  21. **************************************************************************/
  22.  
  23. #include <windows.h>
  24. #include <windowsx.h>
  25. #include <shlobj.h>
  26. #include "globals.h"
  27. #include "resource.h"
  28.  
  29. /**************************************************************************
  30.    Local Function Prototypes
  31. **************************************************************************/
  32.  
  33. BOOL DeleteLink(HWND, LPSTR);
  34. BOOL GetShortcut(HWND, LPSTR, LPSTR);
  35.  
  36. /**************************************************************************
  37.    Global Variables
  38. **************************************************************************/
  39.  
  40. /**************************************************************************
  41.  
  42.    DeleteItem()
  43.  
  44. **************************************************************************/
  45.  
  46. BOOL DeleteItem(HWND hWnd)
  47. {
  48. LPITEMIDLIST   pidlStartMenu;
  49. char           szShortcut[MAX_PATH],
  50.                szPath[MAX_PATH];
  51.  
  52. //get the pidl for the start menu
  53. SHGetSpecialFolderLocation(NULL, CSIDL_STARTMENU, &pidlStartMenu);
  54.  
  55. //get the path for the start menu folder
  56. SHGetPathFromIDList(pidlStartMenu, szPath);
  57.  
  58. if(!GetShortcut(hWnd, szPath, szShortcut))
  59.    return FALSE;
  60.  
  61. if(!DeleteLink(hWnd, szShortcut))
  62.    return FALSE;
  63.  
  64. return TRUE;
  65. }
  66.  
  67. /**************************************************************************
  68.  
  69.    DeleteLink()
  70.  
  71. **************************************************************************/
  72.  
  73. BOOL DeleteLink(HWND hWnd, LPSTR lpszShortcut)
  74. {
  75. char  szFile[MAX_PATH];
  76. SHFILEOPSTRUCT fos;
  77.  
  78. ZeroMemory(szFile, sizeof(szFile));
  79. lstrcpy(szFile, lpszShortcut);
  80.  
  81. ZeroMemory(&fos, sizeof(fos));
  82. fos.hwnd = hWnd;
  83. fos.wFunc = FO_DELETE;
  84. fos.pFrom = szFile;
  85. fos.fFlags = FOF_SILENT | FOF_ALLOWUNDO;   //send to the recycle bin
  86. SHFileOperation(&fos);
  87.  
  88. return TRUE;
  89. }
  90.  
  91. /**************************************************************************
  92.  
  93.    GetShortcut()
  94.  
  95. **************************************************************************/
  96.  
  97. BOOL GetShortcut(HWND hWnd, LPSTR lpszInitDir, LPSTR lpszShortcut)
  98. {
  99. BOOL           ret;
  100. OPENFILENAME   ofn;
  101. char           szFileName[_MAX_PATH] = "",
  102.                szTitleName[_MAX_FNAME + _MAX_EXT] = "";
  103.  
  104.  
  105. static CHAR szFilter[] = "Shortcuts\0*.lnk;*.pif\0";
  106.  
  107. ZeroMemory(&ofn, sizeof(OPENFILENAME));
  108.  
  109. ofn.lStructSize       = sizeof (OPENFILENAME);
  110. ofn.hwndOwner         = hWnd;
  111. ofn.lpstrFilter       = szFilter;
  112. ofn.nFilterIndex      = 0;
  113. ofn.nMaxFile          = _MAX_PATH;
  114. ofn.nMaxFileTitle     = _MAX_FNAME + _MAX_EXT;
  115. ofn.lpstrTitle        = "Select Shortcut to Delete";
  116. ofn.lpstrFile         = szFileName;
  117. ofn.lpstrFileTitle    = szTitleName;
  118. ofn.lpstrInitialDir   = lpszInitDir;
  119. ofn.Flags             = OFN_FILEMUSTEXIST | OFN_PATHMUSTEXIST | OFN_EXPLORER | OFN_NODEREFERENCELINKS;
  120.  
  121. ret = GetOpenFileName (&ofn) ;
  122.  
  123. if(ret)
  124.    {
  125.    //the common dialog adds an extension
  126.    lstrcpy(lpszShortcut, szFileName);
  127.    }
  128.  
  129. return ret;
  130. }
  131.  
  132.